package com.hanbang.mrcm.else_module;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.mrcm.R;
import com.hanbang.mrcm.base.BaseActivity;
import com.hanbang.mrcm.utils.EditHelper;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.hanbang.mrcm.utils.MyToast;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class RegisterActivity extends BaseActivity {
	private EditHelper editHelper = new EditHelper(this);
	private CheckBox checkBox;
	// 验证码按钮倒计时 总时长和间隔时长
	private long totalTime = 60000, intervalTime = 1000;
	private TextView authCodeButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initView();
		setOnClickListener();
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {
		findViewById(R.id.user_register_button).setOnClickListener(
				onClickListener);
		authCodeButton.setOnClickListener(onClickListener);
		findViewById(R.id.protocol).setOnClickListener(onClickListener);
		findViewById(R.id.user_register_treaty).setOnClickListener(
				onClickListener);
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.user_register_button:
				if (editHelper.check()) {
					if (!((EditText) findViewById(R.id.password))
							.getText()
							.toString()
							.trim()
							.equals(((EditText) findViewById(R.id.password2))
									.getText().toString().trim())) {
						MyToast.show(RegisterActivity.this, "两次密码不一致",
								Toast.LENGTH_LONG);
						break;
					}
					if (!checkBox.isChecked()) {
						MyToast.show(RegisterActivity.this, "请同意服务协议",
								Toast.LENGTH_LONG);
						break;
					}
					register();
				}
				break;
			case R.id.user_register_auth_code_button:
				sendYZM();
				break;
			case R.id.user_register_treaty:
			case R.id.protocol:
				Intent intent = new Intent(RegisterActivity.this,
						RegisterProtocolActivity.class);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setBackArrows(findViewById(R.id.top_bar_left_image));
		setTitle(findViewById(R.id.top_bar_title), "用户注册");
		checkBox = (CheckBox) findViewById(R.id.user_register_checkbox);
		authCodeButton = (TextView) findViewById(R.id.user_register_auth_code_button);
		editHelper.setEditText((EditText) findViewById(R.id.nice),
				(EditText) findViewById(R.id.phone),
				(EditText) findViewById(R.id.sms_yzm),
				(EditText) findViewById(R.id.password),
				(EditText) findViewById(R.id.password2));
		editHelper.setHintText("请输入昵称", "请正确输入手机号", "请输入短信验证码", "请输入最少6位密码",
				"请输入最少6位确认密码");
		editHelper.setHintMinNumber(1, 11, 1, 6, 6);

	}

	/*
	 * http://mrcm.hbung.com/tools/UserLogin.ashx?action=user_register&nickname=asa
	 * &mobile=19051196428&code=0532&password=123&againpassword=123 --注册
	 */
	private void register() throws IndexOutOfBoundsException {
		final ArrayList<String> res = editHelper.getText();
		RequestParams params = new RequestParams();
		params.addBodyParameter("action", "user_register");
		params.addBodyParameter("nickname", res.get(0));
		params.addBodyParameter("mobile", res.get(1));
		params.addBodyParameter("code", res.get(2));
		params.addBodyParameter("password", res.get(3));
		params.addBodyParameter("againpassword", res.get(4));
		httpUtils.send(HttpMethod.POST, HttpInterfaces.userLogin, params,
				new MyRequestCallBack<String>(this, "正在注册...") {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								Intent intent = new Intent();
								intent.putExtra("phone", res.get(1));
								intent.putExtra("password", res.get(3));
								setResult(999, intent);
								finish();
							} else {
								MyToast.show(RegisterActivity.this,
										json.getString("msg"),
										Toast.LENGTH_LONG);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(RegisterActivity.this, "注册失败",
									Toast.LENGTH_LONG);
						}
					}
				});
	}

	private void sendYZM() throws IndexOutOfBoundsException {
		final ArrayList<String> res = editHelper.getText();
		if (res.get(1) == null || res.get(1).equals("")
				|| res.get(1).length() < 11) {
			MyToast.show(RegisterActivity.this, "请正确输入手机号！", Toast.LENGTH_LONG);
			return;
		}
		httpUtils.send(HttpMethod.GET,
				HttpInterfaces.userLogin + "action=send_verify_sms_code"
						+ "&type=1" + "&mobile=" + res.get(1),
				new MyRequestCallBack<String>(this, "正在发送验证码...") {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								countDownTimer.start();
							}
							MyToast.show(RegisterActivity.this,
									json.getString("msg"), Toast.LENGTH_LONG);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	CountDownTimer countDownTimer = new CountDownTimer(totalTime, intervalTime) {
		@Override
		public void onFinish() {// 计时完毕时触发
			authCodeButton.setText("重新发送");
			authCodeButton.setEnabled(true);
			// authCodeButton.setClickable(true);
		}

		@Override
		public void onTick(long currentTime) {// 计时过程显示
			// authCodeButton.setClickable(false);
			authCodeButton.setEnabled(false);
			authCodeButton.setText(currentTime / 1000 + "秒");
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
